//
//  MeetingProgressView.swift
//  codelabs
//
//  Created by 雷雷 on 26/05/2025.
//

import SwiftUI
import SwiftData
import ThemeKit
import TimerKit

struct MeetingProgressView: View {
    let secondElasped:Int
    let secondRemaining:Int
    let theme:Theme
    
    private var totalSeconds:Int{
        secondElasped+secondRemaining
    }
    
    private var progress:Double{
        guard totalSeconds > 0 else { return 1 }
        return Double(secondElasped)/Double(totalSeconds)
    }
    
    private var minuteRemaining:Int{
        secondRemaining/60
    }
    
    var body: some View {
        VStack{
            ProgressView(value:progress)
                .progressViewStyle(ScrumProgressViewStyle(theme:theme))
            
            HStack{
                VStack(alignment:.leading){
                    Text("Elasped")
                    Label("\(secondElasped)",systemImage: "hourglass.tophalf.fill")
                }
                
                Spacer()
                
                VStack(alignment:.trailing){
                    Text("Remaining")
                    Label("\(secondRemaining)",systemImage: "hourglass.bottomhalf.fill")
                        .labelStyle(.trailingIcon)
                }
                
                
            }
        }
        .accessibilityElement(children: .ignore)
        .accessibilityLabel("Time remaining")
        .accessibilityValue("\(minuteRemaining) minutes")
    }
}

#Preview(traits: .dailyScrumSampleData) {
    @Previewable @State var timer = ScrumTimer()
    @Previewable @Query(sort:\DailyScrum.title) var scrums:[DailyScrum]
    
    
    MeetingProgressView(secondElasped: timer.secondsElapsed, secondRemaining: timer.secondsRemaining,theme:.buttercup)
        .onAppear{
            timer.reset(lengthInMinutes: scrums[0].lengthInMinutes, attendeeNames: scrums[0].attendees.map{$0.name})
            timer.startScrum()
        }
        .onDisappear{
            timer.stopScrum()
        }
}
